package com.ebelter.bodyfatscale.ui.activity;


import android.content.Intent;
import android.util.TypedValue;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ebelter.bodyfatscale.R;
import com.ebelter.bodyfatscale.adapter.HostoryListAdapter;
import com.ebelter.bodyfatscale.common.Constants;
import com.ebelter.bodyfatscale.moudules.db.Daos;
import com.ebelter.bodyfatscale.moudules.db.beans.HistoryMesureData;
import com.ebelter.bodyfatscale.util.UserSpUtil;
import com.ebelter.bodyfatscale.util.ViewUtils;
import com.ebelter.btlibrary.util.ULog;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class HistoryListActivity extends BaseActivity {

    private static final String TAG = "HistoryListActivity";
    @BindView(R.id.modify_back_bt)
    ImageView back_iv;


    @BindView(R.id.no_history_ly)
    RelativeLayout no_history_ly;

    @BindView(R.id.history_lv)
    ListView history_lv;

    private HostoryListAdapter adapter;




    @Override
    protected int loadLayoutById() {
        return R.layout.historylist_ly;
    }

    @Override
    protected void initViews() {

      //  StatusBarUtil.setDarkStatusIcon(this,false);
        history_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ULog.i(TAG,"----获取到的数据是---"+adapter.getData().get(position));
                Intent deatilIntent=new Intent(HistoryListActivity.this,HistoryItemDetailActivity.class);
                deatilIntent.putExtra(HistoryItemDetailActivity.ITEMDATD,adapter.getData().get(position));
                startActivity(deatilIntent);
            }
        });
    }
    View listFootView;
    TextView noMoreTv;
    @Override
    protected void initData() {

        adapter=new HostoryListAdapter(this);
        history_lv.setAdapter(adapter);

        listFootView = View.inflate(this, R.layout.no_history, null);
        noMoreTv=listFootView.findViewById(R.id.no_data_tv);
        noMoreTv.setText(R.string.No_more);
        noMoreTv.setTextSize(TypedValue.COMPLEX_UNIT_SP,14);
        history_lv.addFooterView(listFootView);
        ViewUtils.hideView(listFootView);

        getHostoryListDatas();
//        history_lv.setOnScrollListener(new AbsListView.OnScrollListener(){
//            @Override
//            public void onScrollStateChanged(AbsListView view, int scrollState){
//                // 当不滚动时
//                if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
//                    // 判断是否滚动到底部
//                    if (view.getLastVisiblePosition() == view.getCount() - 1) {
//                        //加载更多功能的代码
//                        if (System.currentTimeMillis()-lastLoadingTime>3000){
//                            ULog.i(TAG,"---------加载更多");
//                            lastLoadingTime=System.currentTimeMillis();
//                            getHostoryListDatas();
//                        }
//                    }
//                }
//            }
//
//            @Override
//            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
//            }
//        });

    }


    @OnClick({ R.id.modify_back_bt})
    public void btClick(View view){
        switch (view.getId()){
            case R.id.modify_back_bt:
                finish();
                break;
        }
    }



    public void getHostoryListDatas() {



       long userid = UserSpUtil.readLong(Constants.IUser.USER_ID);
       List<HistoryMesureData> datas = Daos.getInstance().getUserDatas(userid,false);

        if (adapter!=null){
                adapter.addData(datas);
            if (adapter.getCount()==0){
                ViewUtils.displayView(no_history_ly);
            }else {
                ViewUtils.hideView(no_history_ly);
            }
        }


    }
}
